package com.spider.admin.store.primary.mapper;

import com.spider.admin.store.primary.model.AppInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface AppMapper {

    @Insert("INSERT INTO t_application_info(pid,app_name,tenant_id,cloud_id,app_url,app_desc,app_original_url,app_status,create_time,update_time) " +
            " VALUES(#{pid},#{appName},#{tenantId},#{cloudId},#{appUrl},#{appDesc},#{appOriginalUrl},#{appStatus},NOW(),NOW())")
    int saveApp(AppInfo appInfo);


    @Update({
            "<script>",
            "UPDATE t_application_info",
            "<set>",
            "update_time=NOW()",
            "<if test='appInfo.appName != null'>,app_name = #{appInfo.appName}</if>",
            "<if test='appInfo.cloudId != null'>,cloud_id = #{appInfo.cloudId}</if>",
            "<if test='appInfo.appUrl != null'>,app_url = #{appInfo.appUrl}</if>",
            "<if test='appInfo.appDesc != null'>,app_desc = #{appInfo.appDesc}</if>",
            "<if test='appInfo.appOriginalUrl != null'>,app_original_url = #{appInfo.appOriginalUrl}</if>",
            "<if test='appInfo.appStatus != null'>,app_status = #{appInfo.appStatus}</if>",
            "</set>",
            "WHERE pid=#{pid}",
            "</script>"
    })
    int updateApp(AppInfo appInfo,String pid);

    @Select("select * from t_application_info where pid=#{pid}")
    @Results(value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "app_name",property = "appName"),
            @Result(column = "tenant_id",property = "tenantId"),
            @Result(column = "cloud_id",property = "cloudId"),
            @Result(column = "app_url",property = "appUrl"),
            @Result(column = "app_desc",property = "appDesc"),
            @Result(column = "app_original_url",property = "appOriginalUrl"),
            @Result(property = "cloudInfo",column = "cloud_id",
                    one=@One(select = "com.spider.admin.store.primary.mapper.CloudMapper.queryCloudById", fetchType = FetchType.EAGER)),
            @Result(column = "app_status",property = "appStatus")
    })
    AppInfo queryAppById(String pid);


    @Select("select * from t_application_info where pid=#{pid} and app_status='active'")
    @Results(value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "app_name",property = "appName"),
            @Result(column = "tenant_id",property = "tenantId"),
            @Result(column = "cloud_id",property = "cloudId"),
            @Result(column = "app_url",property = "appUrl"),
            @Result(column = "app_desc",property = "appDesc"),
            @Result(column = "app_original_url",property = "appOriginalUrl"),
            @Result(column = "app_status",property = "appStatus")
    })
    AppInfo queryActiveAppById(String pid);

    @Select("select count(0) ct from t_application_info where pid=#{pid} and app_status='active'")
    long queryActiveAppCountById(String pid);
    @Select({"<script>",
            "Select count(0) from t_application_info WHERE tenant_id=#{tenantId} ",
            "<if test='appName != null'> AND app_name = #{appName}</if>",
            "<if test='cloudId != null'> AND cloud_id = #{cloudId}</if>",
            "<if test='appStatus != null'> AND app_status = #{appStatus}</if>",
            "</script>"})
    long getAppRecordNum(String tenantId, String appName, String cloudId, String appStatus);

    @Select({"<script>",
            "Select * from t_application_info WHERE tenant_id=#{tenantId} ",
            "<if test='appName != null'> AND app_name = #{appName}</if>",
            "<if test='cloudId != null'> AND cloud_id = #{cloudId}</if>",
            "<if test='appStatus != null'> AND app_status = #{appStatus}</if>",
            " limit #{pageSize} offset #{startRow}",
            "</script>"})
    @Results(value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "app_name",property = "appName"),
            @Result(column = "tenant_id",property = "tenantId"),
            @Result(column = "cloud_id",property = "cloudId"),
            @Result(column = "app_url",property = "appUrl"),
            @Result(column = "app_desc",property = "appDesc"),
            @Result(column = "app_original_url",property = "appOriginalUrl"),
            @Result(property = "cloudInfo",column = "cloud_id",
                    one=@One(select = "com.spider.admin.store.primary.mapper.CloudMapper.queryCloudById", fetchType = FetchType.EAGER)),
            @Result(column = "app_status",property = "appStatus")
    })
    List<AppInfo> queryAppByTenantId(int startRow, Integer pageSize, String tenantId, String appName, String cloudId, String appStatus);

    @Select({"<script>",
            "Select count(0) from t_application_info WHERE pid!=#{pid} ",
            "<if test='appName != null'> AND app_name = #{appName}</if>",
            "<if test='appStatus != null'> AND app_status = #{appStatus}</if>",
            "</script>"})
    long getAppRecordNumByNotId(String pid, String appName,String appStatus);
}

